package org.example.short_link.admin.controller;
import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.example.short_link.admin.common.convention.result.Result;
import org.example.short_link.admin.common.convention.result.Results;
import org.example.short_link.admin.dto.req.UserLoginReqDTO;
import org.example.short_link.admin.dto.req.UserRegisterReqDTO;
import org.example.short_link.admin.dto.req.UserUpdateReqDTO;
import org.example.short_link.admin.dto.resp.UserActualRespDTO;
import org.example.short_link.admin.dto.resp.UserLoginRespDTO;
import org.example.short_link.admin.dto.resp.UserRespDTO;
import org.example.short_link.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 *
 */
@RestController
@RequiredArgsConstructor
public class UserController {


    private final UserService userService;




    /**
     * 根据用户名查找用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));
    }
    /**
     * 根据用户名查找无脱敏用户信息
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username) , UserActualRespDTO.class));

    }

    /**
     * 查询用户是否存在 ， 存在返回false ， 不存在返回true
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hsUserName(@RequestParam("username") String username){
        return Results.success(userService.hsUserName(username));
    }


    /**
     * 注册用户
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }
    /**
     *
     * 修改用户
     * @param requestParam 修改用户请求实体
     * @return
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> Update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO result = userService.login(requestParam);
        return Results.success(result);
    }

    /**
     * 查询用户是否登录
     * @param Token 用户登录 token
     * @return 返回是否登录状态
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checklogin(@RequestParam("username") String username,@RequestParam("token") String Token){
        return Results.success(userService.checkLogin(username, Token));
    }


    /**
     * 删除用户
     * @param username 根据用户名删除
     * @param token 根据token删除
     * @return
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }


}
